package com.imay.base.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

/**
 * @author Jeaman
 *         ViewPager的Fragment适配器
 */
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {

    /**
     * The m fragment list.
     */
    private ArrayList<Fragment> mFragmentList = null;
    private ArrayList<String> mTitleList;

    /**
     * Instantiates a new fragment pager adapter.
     *
     * @param fragmentManager the m fragment manager
     * @param fragmentList     the fragment list
     */
    public SimpleFragmentPagerAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragmentList) {
        super(fragmentManager);
        mFragmentList = fragmentList;
    }

    public SimpleFragmentPagerAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragmentList, ArrayList<String> titleList) {
        super(fragmentManager);
        mFragmentList = fragmentList;
        mTitleList = titleList;
    }

    public SimpleFragmentPagerAdapter(FragmentManager fragmentManager, Fragment... fragments) {
        super(fragmentManager);
        mFragmentList = new ArrayList<>();
        for (Fragment fragment : fragments) {
            mFragmentList.add(fragment);
        }
    }

    public void setTitles(String... titles) {
        mTitleList = new ArrayList<>();
        for (String title : titles) {
            mTitleList.add(title);
        }
    }

    /**
     * 描述：获取数量.
     *
     * @return the count
     * @see android.support.v4.view.PagerAdapter#getCount()
     */
    @Override
    public int getCount() {
        return mFragmentList == null ? 0 : mFragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return (mTitleList == null || position >= mTitleList.size()) ? null : mTitleList.get(position);
    }

    /**
     * 描述：获取索引位置的Fragment.
     *
     * @param position the position
     * @return the item
     * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
     */
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        if (mFragmentList.size() > position) {
            fragment = mFragmentList.get(position);
        }
        return fragment;
    }
}
